<?php

namespace Modules\Asset\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Illuminate\Support\Facades\DB;
use Modules\Activity\Entities\Choushang\Activity as ChoushangActivity;
use Modules\Activity\Entities\Leitai\Activity as LeitaiActivity;
use Modules\Asset\Entities\Package;
use Modules\Activity\Entities\ActivityOrder;

use Modules\User\Entities\User;
use Validator;
use ThrowException;
use Auth;
use Cache;

// 已开出物品
class PackageController extends Controller
{
    // 列表
    public function index(Request $req)
    {
        $options = $req->all();
        $user = Auth::requireLoginUser();

        $options['only'] = [
            'id',
            'uuid',
            'title',
            'sku_total',
            'union_status',
            'union_status_text'
        ];

        $list = Package::getList($options);

        return $list->generateListResponse();
    }

    // 通过订单id或packaegid来获取开出的物品是什么
    public function show(Request $req)
    {
        $user = Auth::requireLoginUser();

        // 使用订单id开启
        $sku_pass_chk_4 = false;
        $sku_pass_chk = false;
        if ($req->input('order_id')) {
            $package = $user->packages()->where('pay_order_id', $req->order_id)->first();

            $order = ActivityOrder::getEntity([
                'id' => $req->order_id
            ]);

            if (!$package) {
                if ($order->union_status === 'closed') {
                    ThrowException::Conflict('付款慢了，赏被抢光了。订单款已退至红包余额~');
                }

                // 尝试创建package
                $package = $order->createPackage() ?: ThrowException::Conflict('奖品排队开启中，请等待几秒后刷新~');
            }

            if ( $order->pay_type ){
                // 抽赏
                if ( $order->node_type == 'choushang' ){

                    $activity = ChoushangActivity::getEntity([
                        'id' => $order->node_id
                    ]);
                    $is_last_open = $activity->checkSkuStockAllOpen();
                    if ( $is_last_open && !$activity->last_open_time ){
                        $half = time() + $activity->last_open_time_set;
                        DB::update('update choushang_activity set last_open_time = ? where id = ? ',[$half, $order->node_id]);
                    }

                    // 全秀
                    if ( $order->is_last == 1 ){
                        // 查询底奖
                        $fudaiSku=DB::table('choushang_last_skus')->where('activity_id', $order->node_id)->where('stock', '>', 0)->first();
                        $fudaiSku = object_to_array($fudaiSku);
                        if ( isset($fudaiSku['id']) ){
                            $pay_order_id = $req->order_id;
                            $package_assets = DB::table('package_assets')->where('pay_order_id',$pay_order_id)->first();
                            $package_assets = object_to_array($package_assets);
                            if ( isset($package_assets['id']) ){
                                $orders = DB::table('package_skus')->where('package_asset_id', $package_assets['id'])->first();
                                $orders = object_to_array($orders);

                                if ( $orders ){
                                    unset($orders['id']);
                                    unset($orders['return_saled_at']);
                                    unset($orders['picked_at']);
                                    unset($orders['resaled_at']);
                                    unset($orders['return_redpack']);
                                    unset($orders['return_score']);
                                    unset($orders['return_money']);
                                    unset($orders['deleted_at']);
                                    $orders['uuid'] = uniqid();
                                    $orders['title'] = $fudaiSku['title'];
                                    $orders['created_at'] = now();
                                    $orders['updated_at'] = now();
                                    $orders['thumb'] = $fudaiSku['thumb'];
                                    $orders['money_price'] = $fudaiSku['money_price'];
                                    $orders['score_price'] = $fudaiSku['score_price'];
                                    $orders['total'] = 1;
                                    $orders['status'] = 1;
                                    $new_order_id = DB::table('package_skus')->insertGetId($orders);

                                    if ( $new_order_id > 0 ){
                                        DB::update('update choushang_last_skus set stock = stock - 1, sales = sales + 1 where activity_id = ? ',[$order->node_id]);
                                    }
                                }

                            }
                        }
                    }
                }

                // 擂台
                if ( $order->node_type == 'leitai' ){
                    $activity = LeitaiActivity::getEntity([
                        'id' => $order->node_id
                    ]);

                    // 玩法2
                    if ( $activity->play_type == 2 ){
                        $is_last_open = $activity->checkSkuStockAllOpen();
                        if ( $is_last_open && !$activity->last_open_time ){
                            $half = time() + $activity->last_open_time_set;
                            DB::update('update leitai_activity set last_open_time = ? where id = ? ',[$half, $order->node_id]);
                        }


                        $node_sales = DB::table('leitai_skus')->where('activity_id', $order->node_id)->first(array(
                            \DB::raw('SUM(stock) as stock'),
                            \DB::raw('SUM(sales) as sales')
                        ));
                        $node_sales = object_to_array($node_sales);

                        $fudaiSku = array();
                        if ( $node_sales['sales'] >= $activity->node_2 ){
                            // 节点2
                            // 查询底奖
                            $fudaiSku=DB::table('leitai_last_skus')->where('activity_id', $order->node_id)->where('stock', '>', 0)->first();
                            $fudaiSku = object_to_array($fudaiSku);
                        }elseif ( $node_sales['sales'] >= $activity->node_1 ){
                            // 节点1
                            // 查询底奖
                            $fudaiSku=DB::table('leitai_last_skus')->where('activity_id', $order->node_id)->where('stock', '>', 1)->first();
                            $fudaiSku = object_to_array($fudaiSku);
                        }

                        if ( isset($fudaiSku['id']) ){
                            $pay_order_id = $req->order_id;
                            $package_assets = DB::table('package_assets')->where('pay_order_id',$pay_order_id)->first();
                            $package_assets = object_to_array($package_assets);
                            if ( isset($package_assets['id']) ){
                                $orders = DB::table('package_skus')->where('package_asset_id', $package_assets['id'])->first();
                                $orders = object_to_array($orders);

                                if ( $orders ){
                                    unset($orders['id']);
                                    unset($orders['return_saled_at']);
                                    unset($orders['picked_at']);
                                    unset($orders['resaled_at']);
                                    unset($orders['return_redpack']);
                                    unset($orders['return_score']);
                                    unset($orders['return_money']);
                                    unset($orders['deleted_at']);
                                    unset($orders['share_status']);
                                    unset($orders['sharer']);
                                    $orders['uuid'] = uniqid();
                                    $orders['title'] = $fudaiSku['title'];
                                    $orders['thumb'] = $fudaiSku['thumb'];
                                    $orders['money_price'] = $fudaiSku['money_price'];
                                    $orders['score_price'] = $fudaiSku['score_price'];
                                    $orders['total'] = 1;
                                    $orders['created_at'] = now();
                                    $orders['updated_at'] = now();
                                    $orders['status'] = 1;
                                    $new_order_id = DB::table('package_skus')->insertGetId($orders);

                                    if ( $new_order_id > 0 ){
                                        DB::update('update leitai_last_skus set stock = stock - 1, sales = sales + 1 where activity_id = ? ',[$order->node_id]);
                                    }
                                }

                            }
                        }
                    }

                    // 玩法：节点随机分配
                    if ( $activity->play_type == 3 || $activity->play_type == 6 ){
                        $is_last_open = $activity->checkSkuStockAllOpen();
                        if ( $is_last_open && !$activity->last_open_time ){
                            $half = time() + $activity->last_open_time_set;
                            DB::update('update leitai_activity set last_open_time = ? where id = ? ',[$half, $order->node_id]);
                        }

                        $node_sales = DB::table('leitai_skus')->where('activity_id', $order->node_id)->first(array(
                            \DB::raw('SUM(stock) as stock'),
                            \DB::raw('SUM(sales) as sales')
                        ));
                        $node_sales = object_to_array($node_sales);

                        $fudaiSku = array();
                        $last_prize_type = 0;
                        $tmp_1 = 0;
                        if ( $node_sales['sales'] >= $activity->node_2 ){
                            $last_prize_type = 7;
                            // 节点2
                            // 查询底奖
                            $fudaiSku=DB::table('leitai_last_skus')->where('activity_id', $order->node_id)->where('last_prize_type', $last_prize_type)->where('stock', '>', 0)->first();
                            $fudaiSku = object_to_array($fudaiSku);
                        }elseif ( $node_sales['sales'] >= $activity->node_1 ){
                            $last_prize_type = 6;
                            $tmp_1 = 1;
                            // 节点1
                            // 查询底奖
                            $fudaiSku=DB::table('leitai_last_skus')->where('activity_id', $order->node_id)->where('last_prize_type', $last_prize_type)->where('stock', '>', 0)->first();
                            $fudaiSku = object_to_array($fudaiSku);
                        }

                        if ( isset($fudaiSku['id']) ){

                            DB::beginTransaction();
                            // 判断是不是有 “ -底奖赠送 ” 说明是第二次开奖
                            $where_lucky_arr = [
                                'node_id'=>$order->node_id,
                                'node_type'=>'leitai'
                            ];
                            $order_last_his =  DB::table('package_assets')->where(array('node_id'=>$order->node_id, 'node_type'=>'leitai', ['title', 'like', '%-底奖赠送']))->first(\DB::raw('max(id) id'));
                            if ( isset($order_last_his->id) && $order_last_his->id > 0 ){
                                array_push($where_lucky_arr, ['package_asset_id', '>', $order_last_his->id]);
                            }

                            $order_his =  DB::table('package_skus')->where($where_lucky_arr)->groupBy('user_id')->get(\DB::raw('user_id, sum(total) total'))->toArray();
                            if ( !$order_his ){
                                $where_lucky_arr = [
                                    'node_id'=>$order->node_id,
                                    'node_type'=>'leitai'
                                ];
                                $order_his =  DB::table('package_skus')->where($where_lucky_arr)->groupBy('user_id')->get(\DB::raw('user_id, sum(total) total'))->toArray();
                            }
                            $user_arr_all = array();
                            foreach ($order_his as $k => $v){
                                for ( $this_i=0; $this_i<$v->total;$this_i++ ){
                                    array_push($user_arr_all, $v->user_id);
                                }
                            }
                            $this_i_nb = rand(0, count($user_arr_all)-1);
                            $user_id = $user_arr_all[$this_i_nb]; // 幸运得主
                            /*
                            if ( $order->node_id == 1049854 && $tmp_1 == 1){
                                // 临时处理
                                $user_id = 18575;
                            }
                            */

                            //查询指定开奖
                            $check_find =  DB::table('fa_check')->where('type', 2)->where('status', 1)->where('node_id', $order->node_id)->first();
                            if ( $check_find && $check_find->user_id && in_array($check_find->user_id, $user_arr_all) ){
                                $user_id = $check_find->user_id;
                                DB::update('update fa_check set status = 2, update_time= "'.date('Y-m-d H:i:s').'" where id = ? ',[$check_find->id]);
                            }

                            $order_his =  DB::table('package_skus')->where(array('node_id'=>$order->node_id, 'user_id'=>$user_id, 'node_type'=>'leitai'))->inRandomOrder()->first();
                            $order_his = object_to_array($order_his);

                            /*
                            $order_his =  DB::table('package_skus')->where(array('node_id'=>$order->node_id, 'node_type'=>'leitai'))->inRandomOrder()->first();
                            $order_his = object_to_array($order_his);
                            $user_id = $order_his['user_id']; // 幸运得主
                            */


                            $order_user_his =  DB::table('orders')->where(array('user_id'=>$user_id, 'node_id'=>$order->node_id, 'node_type'=>'leitai'))->first();
                            $order_user_his = object_to_array($order_user_his);

                            $order_old_id = $order_user_his['id'];
                            unset($order_user_his['id']);
                            $order_user_his['uuid'] = uniqid();
                            $order_user_his['number'] = 'A' . date('mdHis') . $user_id . rand(100, 999);

                            $code = rand(1000, 9999) . strVal(time()) . $user_id . '0' . rand(100000, 999999);

                            $code = substr($code, -16);
                            $order_user_his['code'] = $code;
                            $order_user_his['product_total'] = 1;
                            $order_user_his['sku_total'] = 1;
                            $order_user_his['pay_number'] = 'A' . date('mdHis') . $user_id . rand(100, 999);
                            $order_user_his['confirmed_at'] = date('Y-m-d H:i:s');
                            $order_user_his['money_paid_at'] = date('Y-m-d H:i:s');
                            $order_user_his['completed_at'] = date('Y-m-d H:i:s');
                            $order_user_his['auto_closed_at'] = date('Y-m-d H:i:s');
                            $order_user_his['auto_completed_at'] = date('Y-m-d H:i:s');
                            $order_user_his['created_at'] = date('Y-m-d H:i:s');
                            $order_user_his['updated_at'] = date('Y-m-d H:i:s');

                            $new_orders_id = DB::table('orders')->insertGetId($order_user_his);

                            $order_skus_his =  DB::table('order_skus')->where(array('order_id'=>$order_old_id))->first();
                            $order_skus_his = object_to_array($order_skus_his);
                            unset($order_skus_his['id']);
                            $order_skus_his['uuid'] = uniqid();
                            $order_skus_his['order_id'] = $new_orders_id;
                            $order_skus_his['total'] = 1;
                            $order_skus_his['created_at'] = date('Y-m-d H:i:s');
                            $order_skus_his['updated_at'] = date('Y-m-d H:i:s');
                            DB::table('order_skus')->insertGetId($order_skus_his);


                            $package_assets = DB::table('package_assets')->where(array('id'=>$order_his['package_asset_id']))->first();
                            $package_assets = object_to_array($package_assets);

                            unset($package_assets['id']);
                            $package_assets['uuid'] = uniqid();
                            $package_assets['title'] .= '-底奖赠送';
                            $package_assets['sku_total'] = 1;
                            $package_assets['pay_order_id'] = $new_orders_id;
                            $package_assets['created_at'] = date('Y-m-d H:i:s');
                            $package_assets['updated_at'] = date('Y-m-d H:i:s');
                            $package_assets_new_id = DB::table('package_assets')->insertGetId($package_assets);


                            unset($order_his['id']);
                            unset($order_his['return_saled_at']);
                            unset($order_his['picked_at']);
                            unset($order_his['resaled_at']);
                            unset($order_his['return_redpack']);
                            unset($order_his['return_score']);
                            unset($order_his['return_money']);
                            unset($order_his['deleted_at']);
                            $order_his['uuid'] = uniqid();
                            $order_his['title'] = $fudaiSku['title'];
                            $order_his['thumb'] = $fudaiSku['thumb'];
                            $order_his['money_price'] = $fudaiSku['money_price'];
                            $order_his['score_price'] = $fudaiSku['score_price'];
                            $order_his['total'] = 1;
                            $order_his['status'] = 1;
                            $order_his['created_at'] = now();
                            $order_his['updated_at'] = now();
                            $order_his['package_asset_id'] = $package_assets_new_id;
                            $new_order_id = DB::table('package_skus')->insertGetId($order_his);

                            if ( $new_order_id > 0 ){
                                DB::update('update leitai_last_skus set stock = stock - 1, sales = sales + 1 where activity_id = ? and last_prize_type = ?',[$order->node_id, $last_prize_type]);
                            }
                            DB::commit();
                        }
                    }

                    // 玩法：空空奖
                    if ( $activity->play_type == 4 ){
                        // 玩法4
                        $this->playType4($order->node_id);
                        $sku_pass_chk_4 = true;
                        // echo json_encode(array('code'=>201, 'msg'=>'购买成功，已开奖~'));die;

                        // 空车数据验证
                        $chk = DB::table('package_skus')->where('node_id', $order->node_id)->groupBy('user_id')->groupBy('node_title')->select(\DB::raw('count(*) c, user_id, node_title'))->get()->toArray();
                        foreach ( $chk as $k => $v ){
                            if ( $v->c <= 1 ) continue;
                            // 开奖数 大于 1 空车清零
                            $kongche_pd = DB::table('kongche')->where(array('activity_title'=>$v->node_title, 'user_id'=>$v->user_id ))->first();
                            if ( isset($kongche_pd->nums) && $kongche_pd->nums > 0 ){
                                DB::update("update kongche set nums = 0  where id = {$kongche_pd->id}");
                            }
                        }
                    }

                    // 玩法：PK秀池
                    if ( $activity->play_type == 5 ){
                        $this->playType5($order->node_id);
                        $sku_pass_chk = true;
                    }

                    // 玩法：空空奖PRO 单人可多开
                    if ( $activity->play_type == 7 ){
                        $sku_pass_chk_4 = true;
                        $this->playType7($order->node_id);
                        $sku_pass_chk = true;
                    }
                }
            }

        } elseif ($req->input('package_id')) {
            $package = $user->packages()->where('id', $req->package_id)->first() ?: ThrowException::Conflict('奖品排队开启中，请稍后再试~');
        } else {
            ThrowException::Conflict('请指定订单id或package id');
        }

        $info = $package->getInfo([
            'id',
            'uuid',
            'title',
            'union_status',
            'union_status_text',
            'reward'
        ]);

        // 仅已开盒的package可查看sku列表
        if ($package->union_status === 'opened') {

            // 仅返回的字段
            $only = [
                'id',
                'uuid',
                'title',
                'number',
                'thumb',
                'money_price',
                'score_price',
                'total',
                'options',
                'union_status',
                'sku_type',
                'sku_level',
                'node_type',
                'node_id',
                'user_id',
                'sku_id',
                'sku_level',
                'package_asset_id',
                'union_status_text',
                'sid'
            ];
            // if($item->sku_id){
            //     $fudaiSku=DB::table('fudai_skus')->where('id',$item->sku_id)->first();
                $fudaiTypeArray = ['普通款','隐藏款','稀有款','超神款','欧皇款'];
                // $fudaiTypestr =$fudaiTypeArray[$fudaiSku->sku_type+1]; 
            // }
            $skusArray= filtList($package->skus()->take(50)->get(), $only);

            // 大魔王标识
            $dmw_mark = 0;
            $info['dmw_mark'] = 0;
            $row_dmw = array();

            $pass_arr = array();
            if ( $sku_pass_chk ){
                $pass_sku = DB::table('leitai_last_skus')->where('activity_id', $order->node_id)->where('last_prize_type', 5)->select('id')->get()->toArray();

                if ( count($pass_sku) > 0 ){
                    $pass_arr = array_column($pass_sku, 'id');
                }
            }

            $fd_dmw = DB::select("select id, is_dmw_enabled, dmw_level, dmw_ratio, dmw_type, dmw_time_limit from fudai_activity where id = {$skusArray[0]['node_id']} and is_dmw_enabled = 1 ");
            if ( count($fd_dmw) ){
                $fd_dmw_res = array_map('get_object_vars', $fd_dmw) ;
                $row_dmw = $fd_dmw_res[0];
            }

            foreach($skusArray as $k => &$item){
                if ( $pass_arr && in_array($item['sku_id'], $pass_arr ) ){
                    unset($skusArray[$k]);
                    unset($item);
                    continue;
                }

                // 屏蔽空空奖玩法时间差导致提前看到底奖
                if ( $sku_pass_chk_4 && $item['sku_level'] != 1 ){
                    unset($skusArray[$k]);
                    unset($item);
                    continue;
                }

                if ( !in_array($item['node_type'], array('leitai', 'choushang') ) )
                    $item['title'] = '【'.$fudaiTypeArray[$item['sku_level'] - 1].'】'.$item['title'];

                $names_ = DB::select("select * from dmw_reward where node_id = {$item['node_id']} and status = 1 ");
                $names = false;
                if ( count($names_) > 0 ){
                    $names_1 = array_map('get_object_vars', $names_) ;
                    $names = $names_1[0];
                }

                if ( $item['node_type'] == 'fudai' && $dmw_mark == 0 && isset($row_dmw['is_dmw_enabled']) && $row_dmw['is_dmw_enabled'] == 1){
                    if ( $item['sku_level'] >= $row_dmw['dmw_level']){
                        // 触发大魔王属性
                        if( $names ){
                            DB::update('update dmw_reward set status = 2, over_time = "'.date('Y-m-d H:i:s').'" where node_id = ? and status = 1',[$item['node_id']]);
                        }

                        $insert = array(
                            'create_time'       => date('Y-m-d H:i:s'),
                            'user_id'           => $item['user_id'],
                            'package_asset_id'  => $item['package_asset_id'],
                            'node_id'           => $item['node_id'],
                            'status'            => 1,
                            'get_reward'        => 0,
                            'issued_reward'     => 0,
                        );
                        if ( isset($row_dmw['dmw_time_limit']) && $row_dmw['dmw_time_limit'] > 0 ){
                            $insert['max_time'] = date('Y-m-d H:i:s', strtotime("+ {$row_dmw['dmw_time_limit']} hour"));
                        }else{
                            $insert['max_time'] = '2099-12-30';
                        }

                        DB::table('dmw_reward')->insert($insert);

                        $info['dmw_mark'] = 1;
                        $dmw_mark = 1;
                    }else{
                        if( $names != false && $names['user_id'] != $item['user_id'] ){
                            // 给大魔王++
                            DB::update('update dmw_reward set get_reward = get_reward+1 where node_id = ? and status = 1 and max_time >= ? and user_id <> ?'
                                ,[$item['node_id'], date('Y-m-d H:i:s'), $item['user_id']]);

                            if ( $row_dmw['dmw_type'] == 1 ){
                                // 积分
                                $jf = $row_dmw['dmw_ratio'];
                                DB::update('update dmw_reward set issued_reward = issued_reward+'.$jf.' where node_id = ? and status = 1 and max_time >= ? and user_id <> ?'
                                    ,[$item['node_id'], date('Y-m-d H:i:s'), $item['user_id']]);

                                $user_dmw = User::getEntity([
                                    'id' => $names['user_id']
                                ], false);
                                $user_dmw->asset('score')->in($jf, [
                                    'description' => '擂主奖励_'.$info['title']
                                ]);

                                // DB::update('update assets set balance = balance+'.$jf.' where name = "score" and target_id = ?' ,[$names['user_id']]);
                            }
                            if ( $row_dmw['dmw_type'] == 2 ){
                                // 红包
                                $jf = $row_dmw['dmw_ratio']*100;
                                DB::update('update dmw_reward set issued_reward = issued_reward+'.$jf.' where node_id = ? and status = 1 and max_time >= ? and user_id <> ?'
                                    ,[$item['node_id'], date('Y-m-d H:i:s'), $item['user_id']]);

                                $user_dmw = User::getEntity([
                                    'id' => $names['user_id']
                                ], false);
                                $user_dmw->asset('redpack')->in($jf, [
                                    'description' => '擂主奖励_'.$info['title']
                                ]);
                                // DB::update('update assets set balance = balance+'.$jf.' where name = "redpack" and target_id = '.$names['user_id']);
                            }
                        }
                    }
                }
            }
            $info['skus'] = $skusArray;
            $info['free_order_total'] = $package->skus()->sum('free_order_total');
        }
        else {
            $info['skus'] = [];
        }

        return $info;
    }

    public function getOrderPrize($order_id, $user)
    {
        // 使用订单id开启
        $package = $user->packages()->where('pay_order_id', $order_id)->first();

        $order = ActivityOrder::getEntity([
            'id' => $order_id
        ]);

        if (!$package) {
            if ($order->union_status === 'closed') {
//                ThrowException::Conflict('付款慢了，赏被抢光了。订单款已退至红包余额~');
                return false;
            }

            // 尝试创建package
//            $package = $order->createPackage() ?: ThrowException::Conflict('奖品排队开启中，请等待几秒后刷新~');
            $package = $order->createPackage() ;
            if ( !$package ){
                return false;
            }
        }

        // 抽赏
        if ( $order->pay_type ){
            // 擂台
            if ( $order->node_type == 'leitai' ){
                $activity = LeitaiActivity::getEntity([
                    'id' => $order->node_id
                ]);

                // 玩法：空空奖
                if ( $activity->play_type == 4 ){
                    // 玩法4
                    // 机器人可能开的太快， 这里检查是否超卖
                    $this->playType4($order->node_id);

                    // 空车数据验证
                    $chk = DB::table('package_skus')->where('node_id', $order->node_id)->groupBy('user_id')->groupBy('node_title')->select(\DB::raw('count(*) c, user_id, node_title'))->get()->toArray();
                    foreach ( $chk as $k => $v ){
                        if ( $v->c <= 1 ) continue;
                        // 开奖数 大于 1 空车清零
                        $kongche_pd = DB::table('kongche')->where(array('activity_title'=>$v->node_title, 'user_id'=>$v->user_id ))->first();
                        if ( isset($kongche_pd->nums) && $kongche_pd->nums > 0 ){
                            DB::update("update kongche set nums = 0  where id = {$kongche_pd->id}");
                        }
                    }
                }

                // 玩法：PK秀池
                if ( $activity->play_type == 5 ){
                    $this->playType5($order->node_id);
                    $sku_pass_chk = true;
                }
            }
        }
        return true;
    }


    public function playType4($activity_id, $chk_mark=1)
    {
        // 先检查上一箱是不是开了
        if ( $chk_mark === 1 ){
            $activity_id_chk = $activity_id - 1;
            self::playType4($activity_id_chk, 0);
        }

        $activity = LeitaiActivity::where([
            'id' => $activity_id,
            'play_type' => 4
        ])->first();

        if ( !$activity ){
            return false;
        }

        $chk = DB::table('orders')->where('status', '<>', 2)->where('node_uuid', $activity->uuid)->whereNotNull('pay_type')->whereNotNull('completed_at')->whereIn('pay_status', array(2, 3))->whereNull('closed_by')->get(\DB::raw('count(*) c'))->toArray();
        if ( $chk[0]->c > $activity->open_value && 0){
            // 超卖了
            $job_order = DB::table('orders')->where(['node_uuid'=> $activity->uuid, 'remark'=> 'job order'])->first('id');

            if ( $job_order && $job_order->id && in_array($chk_mark, [0, 2]) ){
                DB::table('package_skus')->where('pay_order_id', '=', $job_order->id)->delete();
                DB::table('package_assets')->where('pay_order_id', '=', $job_order->id)->delete();
                DB::table('orders')->where('id', '=', $job_order->id)->delete();
            }
        }

        $chk = DB::table('package_skus')->where('node_uuid', $activity->uuid)->select(\DB::raw('count(distinct title) c'))->get()->toArray();
        if ( $chk[0]->c > 1 ){
            // 应该是已经开完了
            return false;
        }

        // 是否满足开奖条件
        $chk = DB::table('leitai_last_skus')->where([
            'activity_id' => $activity_id,
            'last_prize_type' => 3,
            'stock' => 0,
        ])->first('id');
        if ( !$chk || !($chk->id > 0) ){
            return false;
        }

        $oper_chk_arr = array(
            'node_id' => $activity_id,
            'node_uuid' => $activity->uuid,
            'node_type' => 'leitai',
            'sku_id' => $chk->id,
        );

        $order_list = DB::table('package_skus')->where($oper_chk_arr)->select(['pay_order_id', 'user_id'])->get()->toArray();

        /*
        // 是否满足开奖条件
        $oper_chk_arr = array(
            'node_id' => $activity_id,
            'node_type' => 'leitai'
        );
        $order_list = DB::table('orders')->where($oper_chk_arr)->whereNull('closed_by')->whereNotNull('pay_type')->whereIn('pay_status', array(2, 3))->select(['id', 'user_id'])->get()->toArray();
        */
        $order_cnt = count($order_list);
        if ( $order_cnt >= $activity->open_value ){
            DB::beginTransaction();

            // 开奖
            $sku_arr = DB::table('leitai_skus')->where(array('activity_id'=>$activity_id))->get();
            $prize_arr = array();

            // 小黑屋人员
            $black_house = DB::select("select user_id from fa_black_house");
            $black_house = array_column($black_house, 'user_id');

            $black_house_this = 0;
            foreach ( $order_list as $k => $v ){
                if ( in_array($v->user_id, $black_house) ){
                    $black_house_this++;
                }
            }

            // 如果都是小黑屋人员 清空小黑屋
            if ( count($black_house) == $black_house_this ) $black_house = array();

            $max_index = $order_cnt-1;

            foreach ( $sku_arr as $sku_item ){

                for ( $i=0;$i<$sku_item->stock;$i++ ){
                    // 中奖 下标
                    $prize_index = rand(0, $max_index);
                    isset($prize_arr[$order_list[$prize_index]->pay_order_id]) ?: $prize_arr[$order_list[$prize_index]->pay_order_id]=0;
                    $prize_arr[$order_list[$prize_index]->pay_order_id]++;

                    // 分配普通奖
                    $this->setLeitaiPrize($activity, $order_list[$prize_index]->pay_order_id, $sku_item, $order_list[$prize_index]->user_id, 'leitai_skus', 1, 2);

                    if ( in_array($order_list[$prize_index]->user_id, $black_house) ){
                        unset($order_list[$prize_index]);
                        sort($order_list);
                        $max_index = $max_index - 1;
                    }
                }
            }

            // 一串多
            $sku_last_arr = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>4))->get()->toArray();
            if ( count($sku_last_arr) > 0 ){
                foreach ( $sku_last_arr as $k => $v ){
                    if ( $v->last_prize_type == 4 && $v->more_value1 > 0 ){
                        $prize_arr_type4 = DB::table('package_skus')->where(array('node_id'=>$activity_id, 'node_type'=>'leitai', 'sku_level'=>9))->groupBy('user_id')->get(
                            \DB::raw('user_id, max(pay_order_id) pay_order_id, sum(total) cnt')
                        )->toArray();
                        foreach ( $prize_arr_type4 as $prize_info_k => $prize_info_v ){
                            if ( $prize_info_v->cnt >= $v->more_value1 && $prize_info_v->cnt <= $v->more_value2 ){
                                // 中奖订单
                                $this->setLeitaiPrize($activity, $prize_info_v->pay_order_id, $v, $prize_info_v->user_id, 'leitai_last_skus');
                            }
                        }
                    }
                }
            }

            // 空空奖
            $kong_chk_arr = array(
                'node_id' => $activity_id,
                'sku_level' => 1,
                'node_type' => 'leitai'
            );

            $get_chk_arr = array(
                'node_id' => $activity_id,
                'sku_level' => 9,
                'node_type' => 'leitai'
            );

            // 空空奖列表
            $sku_last_kongche = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>2, array('kong_value', '>', 0)))->get()->toArray();

            $sku_user_list = DB::table('package_skus')->where($kong_chk_arr)->groupBy('user_id')->select(['user_id', \DB::raw('max(pay_order_id) pay_order_id')])->get()->toArray();
            foreach ( $sku_user_list as $k => $v ){
                // 检查是否已经执行过
                $kongche_log_pd = DB::table('kongche_log')->where(array('node_id'=>$activity->id, 'title'=>$activity->title, 'user_id'=>$v->user_id ))->first();
                if ( $kongche_log_pd ){
                    continue;
                }

                // 是否有记录
                $kongche_pd = DB::table('kongche')->where(array('activity_title'=>$activity->title, 'user_id'=>$v->user_id ))->first();

                $get_chk_arr['user_id'] = $v->user_id;
                // 判断是否得奖
                $sku_user_chk = DB::table('package_skus')->where($get_chk_arr)->select('id')->first();
                if ( $sku_user_chk ){
                    // 得奖人 空车数清零
                    if ( $kongche_pd ){
                        DB::update("update kongche set nums = 0  where id = {$kongche_pd->id}");

                        $kongche_his = array();
                        $kongche_his['create_time'] = date('Y-m-d H:i:s');
                        $kongche_his['user_id'] = $v->user_id;
                        $kongche_his['node_id'] = $activity_id;
                        $kongche_his['title'] = $activity->title;
                        $kongche_his['txt'] = '清零';
                        DB::table('kongche_log')->insert($kongche_his);
                    }
                }else{
                    // 未得奖 空车数+1
                    if ( $kongche_pd ){
                        $log_n = $kongche_pd->nums;
                        $kong_table_id = $kongche_pd->id;
                        DB::update("update kongche set nums = nums +1  where id = {$kongche_pd->id}");
                    }else{
                        $log_n = 0;
                        $kongche_ins = array();
                        $kongche_ins['activity_title'] = $activity->title;
                        $kongche_ins['user_id'] = $v->user_id;
                        $kongche_ins['nums'] = 1;
                        $kong_table_id = DB::table('kongche')->insertGetId($kongche_ins);
                    }

                    $kongche_his = array();
                    $kongche_his['create_time'] = date('Y-m-d H:i:s');
                    $kongche_his['user_id'] = $v->user_id;
                    $kongche_his['node_id'] = $activity_id;
                    $kongche_his['title'] = $activity->title;
                    $kongche_his['txt'] = $log_n.'+1';
                    DB::table('kongche_log')->insert($kongche_his);

                    // 判断是否中了空空奖
                    if ( $sku_last_kongche ){
                        foreach ( $sku_last_kongche as $kong_k => $kong_v ){
                            $kong_now = $log_n+1; // 当前空车数
                            if ( $kong_now == $kong_v->kong_value ){
                                // 中奖分发
                                $this->setLeitaiPrize($activity, $v->pay_order_id, $kong_v, $v->user_id, 'leitai_last_skus');

                                // 中奖后空车数清零 并加入日志
                                DB::update("update kongche set nums =0  where id = {$kong_table_id}");
                                $kongche_his = array();
                                $kongche_his['create_time'] = date('Y-m-d H:i:s');
                                $kongche_his['user_id'] = $v->user_id;
                                $kongche_his['node_id'] = $activity_id;
                                $kongche_his['title'] = $activity->title;
                                $kongche_his['txt'] = '发放后清零';
                                DB::table('kongche_log')->insert($kongche_his);
                            }
                        }
                    }
                }
            }

            DB::commit();
//            echo json_encode(array('code'=>200, 'msg'=>'购买成功，已开奖~'));die;
        }

        return false;
//        echo json_encode(array('code'=>201, 'msg'=>'购买成功，等待开奖！'));die;
    }

    public function playType5($activity_id)
    {
        $activity = LeitaiActivity::getEntity([
            'id' => $activity_id,
            'play_type' => 5
        ]);

        if ( !$activity ){
            return false;
        }

        // 是否满足开奖条件
        $chk = DB::table('leitai_skus')->where([
            'activity_id' => $activity_id,
            'stock' => 0,
        ])->first('id');
        if ( !$chk || !($chk->id > 0) ){
            return false;
        }
        // $order_list = DB::table('orders')->where($oper_chk_arr)->whereNull('closed_by')->whereNotNull('pay_type')->whereIn('pay_status', array(2, 3))->select(['id', 'user_id'])->get()->toArray();

        $oper_chk_arr = array(
            'node_id' => $activity_id,
            'sku_id' => $chk->id,
            'node_type' => 'leitai'
        );
        $order_list = DB::table('package_skus')->where($oper_chk_arr)->select(['pay_order_id', 'user_id'])->get()->toArray();

        $order_cnt = count($order_list);
        if ( $order_cnt == 2 ){
            DB::beginTransaction();

            $sku_last_arr = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>5))->get()->toArray();
            if ( count($sku_last_arr) > 0 ){
                $index_this = 1;
                foreach ( $sku_last_arr as $k => $v ){
                    if ( $k == 0 ){
                        $prize_index = rand(0, $order_cnt-1);
                        $order_this = $order_list[$prize_index];
                        $index_this -= $prize_index;
                    }else{
                        $order_this = $order_list[$index_this];
                    }
                    $this->setLeitaiPrize($activity, $order_this->pay_order_id, $v, $order_this->user_id, 'leitai_last_skus', 0, 0, 1);

                    if ( $v->show_to == 1 ){
                        // 秀池奖品
                        $last_prize_type = 6;
                    }else{
                        // show奖品
                        $last_prize_type = 7;
                    }
                    $show_sku_list = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>$last_prize_type))->get()->toArray();
                    $sku_id_arr = array_column($show_sku_list, 'odds', 'id');
                    asort($sku_id_arr);
                    $sku_id = $this->get_rand($sku_id_arr);
                    $show_sku_prize = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>$last_prize_type, 'id'=>$sku_id ))->first();
                    $this->setLeitaiPrize($activity, $order_this->pay_order_id, $show_sku_prize, $order_this->user_id, 'leitai_last_skus', 0);
                }
            }

            DB::commit();
        }

        return true;
    }

    public function playType7($activity_id)
    {
        $activity = LeitaiActivity::getEntity([
            'id' => $activity_id,
            'play_type' => 7
        ]);

        if ( !$activity ){
            return false;
        }

        $chk = DB::table('package_skus')->where('node_uuid', $activity->uuid)->select(\DB::raw('count(distinct title) c'))->get()->toArray();
        if ( $chk[0]->c > 1 ){
            // 应该是已经开完了 或者 正在开
            return false;
        }

        // 是否满足开奖条件
        $chk = DB::table('leitai_last_skus')->where([
            'activity_id' => $activity_id,
            'last_prize_type' => 3,
            'stock' => 0,
        ])->first('id');
        if ( !$chk || !($chk->id > 0) ){
            return false;
        }

        // 开奖
        $oper_chk_arr = array(
            'node_id' => $activity_id,
            'node_type' => 'leitai',
            'sku_id' => $chk->id,
        );
        $order_list = DB::table('package_skus')->where($oper_chk_arr)->select(['total', 'pay_order_id', 'user_id'])->get()->toArray();
        $order_arr = array();
        foreach ( $order_list as $k => $v ){
            for( $i=0;$i<$v->total;$i++ ){
                $order_arr[] = [
                    'user_id' => $v->user_id,
                    'pay_order_id' => $v->pay_order_id,
                ];
            }
        }

        $order_cnt = count($order_arr);
        DB::beginTransaction();

        // 开奖
        $sku_arr = DB::table('leitai_skus')->where(array('activity_id'=>$activity_id))->get();

        // 小黑屋人员
        $black_house = DB::select("select user_id from fa_black_house");
        $black_house = array_column($black_house, 'user_id');

        $black_house_this = 0;
        foreach ( $order_list as $k => $v ){
            if ( in_array($v->user_id, $black_house) ){
                $black_house_this++;
            }
        }

        // 如果都是小黑屋人员 清空小黑屋
        if ( count($black_house) == $black_house_this ) $black_house = array();

        $max_index = $order_cnt-1;

        foreach ( $sku_arr as $sku_item ){
            for ( $i=0;$i<$sku_item->stock;$i++ ){
                // 中奖 下标
                $prize_index = rand(0, $max_index);

                // 分配普通奖
                $this->setLeitaiPrize($activity, $order_arr[$prize_index]['pay_order_id'], $sku_item, $order_arr[$prize_index]['user_id'], 'leitai_skus', 1, 2);
                if ( in_array($order_arr[$prize_index]['user_id'], $black_house) ){
                    unset($order_arr[$prize_index]);
                    sort($order_arr);
                    $max_index = $max_index - 1;
                }
            }
        }

        // 一串多
        $sku_last_arr = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>4))->get()->toArray();
        if ( count($sku_last_arr) > 0 ){
            foreach ( $sku_last_arr as $k => $v ){
                if ( $v->last_prize_type == 4 && $v->more_value1 > 0 ){
                    $prize_arr_type4 = DB::table('package_skus')->where(array('node_id'=>$activity_id, 'node_type'=>'leitai', 'sku_level'=>9))->groupBy('user_id')->get(
                        \DB::raw('user_id, max(pay_order_id) pay_order_id, sum(total) cnt')
                    )->toArray();
                    foreach ( $prize_arr_type4 as $prize_info_k => $prize_info_v ){
                        if ( $prize_info_v->cnt >= $v->more_value1 && $prize_info_v->cnt <= $v->more_value2 ){
                            // 中奖订单
                            $this->setLeitaiPrize($activity, $prize_info_v->pay_order_id, $v, $prize_info_v->user_id, 'leitai_last_skus');
                        }
                    }
                }
            }
        }

        // 空空奖
        $kong_chk_arr = array(
            'node_id' => $activity_id,
            'sku_level' => 1,
            'node_type' => 'leitai'
        );

        $get_chk_arr = array(
            'node_id' => $activity_id,
            'sku_level' => 9,
            'node_type' => 'leitai'
        );

        // 空空奖列表
        $sku_last_kongche = DB::table('leitai_last_skus')->where(array('activity_id'=>$activity_id, 'last_prize_type'=>2, array('kong_value', '>', 0)))->get()->toArray();
        if ( count($sku_last_kongche) > 0 ){
            $sku_user_list = DB::table('package_skus')->where($kong_chk_arr)->groupBy('user_id')->select(['user_id', \DB::raw('max(pay_order_id) pay_order_id')])->get()->toArray();
            foreach ( $sku_user_list as $k => $v ){
                // 检查是否已经执行过
                $kongche_log_pd = DB::table('kongche_log')->where(array('node_id'=>$activity->id, 'title'=>$activity->title, 'user_id'=>$v->user_id ))->first();
                if ( $kongche_log_pd ){
                    continue;
                }

                // 是否有记录
                $kongche_pd = DB::table('kongche')->where(array('activity_title'=>$activity->title, 'user_id'=>$v->user_id ))->first();

                $get_chk_arr['user_id'] = $v->user_id;
                // 判断是否得奖
                $sku_user_chk = DB::table('package_skus')->where($get_chk_arr)->select('id')->first();
                if ( $sku_user_chk ){
                    // 得奖人 空车数清零
                    if ( $kongche_pd ){
                        DB::update("update kongche set nums = 0  where id = {$kongche_pd->id}");

                        $kongche_his = array();
                        $kongche_his['create_time'] = date('Y-m-d H:i:s');
                        $kongche_his['user_id'] = $v->user_id;
                        $kongche_his['node_id'] = $activity_id;
                        $kongche_his['title'] = $activity->title;
                        $kongche_his['txt'] = '清零';
                        DB::table('kongche_log')->insert($kongche_his);
                    }
                }else{
                    // 未得奖 空车数+1
                    if ( $kongche_pd ){
                        $log_n = $kongche_pd->nums;
                        $kong_table_id = $kongche_pd->id;
                        DB::update("update kongche set nums = nums +1  where id = {$kongche_pd->id}");
                    }else{
                        $log_n = 0;
                        $kongche_ins = array();
                        $kongche_ins['activity_title'] = $activity->title;
                        $kongche_ins['user_id'] = $v->user_id;
                        $kongche_ins['nums'] = 1;
                        $kong_table_id = DB::table('kongche')->insertGetId($kongche_ins);
                    }

                    $kongche_his = array();
                    $kongche_his['create_time'] = date('Y-m-d H:i:s');
                    $kongche_his['user_id'] = $v->user_id;
                    $kongche_his['node_id'] = $activity_id;
                    $kongche_his['title'] = $activity->title;
                    $kongche_his['txt'] = $log_n.'+1';
                    DB::table('kongche_log')->insert($kongche_his);

                    // 判断是否中了空空奖
                    if ( $sku_last_kongche ){
                        foreach ( $sku_last_kongche as $kong_k => $kong_v ){
                            $kong_now = $log_n+1; // 当前空车数
                            if ( $kong_now == $kong_v->kong_value ){
                                // 中奖分发
                                $this->setLeitaiPrize($activity, $v->pay_order_id, $kong_v, $v->user_id, 'leitai_last_skus');

                                // 中奖后空车数清零 并加入日志
                                DB::update("update kongche set nums =0  where id = {$kong_table_id}");
                                $kongche_his = array();
                                $kongche_his['create_time'] = date('Y-m-d H:i:s');
                                $kongche_his['user_id'] = $v->user_id;
                                $kongche_his['node_id'] = $activity_id;
                                $kongche_his['title'] = $activity->title;
                                $kongche_his['txt'] = '发放后清零';
                                DB::table('kongche_log')->insert($kongche_his);
                            }
                        }
                    }
                }
            }
        }

        DB::commit();

        return false;
//        echo json_encode(array('code'=>201, 'msg'=>'购买成功，等待开奖！'));die;
    }

    public function setLeitaiPrize($activity, $order_id, $sku_item, $user_id, $stock_db = 'leitai_skus', $set_kong_=1, $is_baodi = 0, $other=0)
    {

        $stock = DB::table($stock_db)->where('id', $sku_item->id)->where('stock', '>', 0)->first();
        if ( !$stock && $is_baodi == 0 ){
            return false;
        }
        $package = DB::table('package_assets')->where(array('node_id'=>$activity->id, 'pay_order_id'=>$order_id))->first();

        if ( !$package ){
            $arr = array();
            $arr['uuid'] = uniqid();
            $arr['title'] = $activity->title;
            $arr['user_id'] = $user_id;
            $arr['node_money_price'] = $activity->money_price;
            $arr['node_score_price'] = $activity->score_price;
            $arr['activity_id'] = $activity->id;
            $arr['node_id'] = $activity->id;
            $arr['activity_type'] = 'leitai';
            $arr['node_type'] = 'leitai';
            $arr['activity_uuid'] = $activity->uuid;
            $arr['node_uuid'] = $activity->uuid;
            $arr['status'] = 1;
            $arr['sku_total'] = 1;
            $arr['pay_order_id'] = $order_id;
            $arr['created_at'] = date('Y-m-d H:i:s');
            $arr['updated_at'] = date('Y-m-d H:i:s');
            $package_id = DB::table('package_assets')->insertGetId($arr);
        }else{
            DB::update('update package_assets set sku_total = sku_total + 1 where id = ? ',[$package->id]);
            $package_id = $package->id;
        }

        $arr = array();
        $arr['uuid'] = uniqid();
        $arr['title'] = $sku_item->title;
        $arr['package_asset_id'] = $package_id;
        $arr['user_id'] = $user_id;
        $arr['thumb'] = $sku_item->thumb;
        $arr['money_price'] = $sku_item->money_price;
        $arr['score_price'] = $sku_item->score_price;
        $arr['sku_type'] = $sku_item->sku_type;
        $arr['pay_order_id'] = $order_id;
        $arr['total'] = 1;
        $arr['status'] = 1;
        $arr['created_at'] = date('Y-m-d H:i:s');
        $arr['updated_at'] = date('Y-m-d H:i:s');
        $arr['node_title'] = $activity->title;
        $arr['node_money_price'] = $activity->money_price;
        $arr['node_score_price'] = $activity->score_price;
        $arr['node_id'] = $activity->id;
        $arr['node_type'] = 'leitai';
        $arr['node_uuid'] = $activity->uuid;
        $arr['sku_id'] = $sku_item->id;
        if ( $is_baodi == 1 ){
            $arr['sku_level'] = 8;
        }

        if ( $is_baodi == 2 ){
            $arr['sku_level'] = 9;
        }

        if ( $other == 1 ){
            $arr['status'] = 9;
        }

        $package_sku_id = DB::table('package_skus')->insertGetId($arr);

        $sql = "update $stock_db set stock = stock - 1, sales = sales + 1 where id = ".$sku_item->id;
        DB::update($sql);

        $stock_chk = DB::table($stock_db)->where(array('id'=>$sku_item->id ))->first('stock');
        $stock_chk = object_to_array($stock_chk);
        if ( isset($stock_chk['stock']) && $stock_chk['stock'] < 0 ){
            // 多分了 撤回
            DB::table('package_skus')->where('id',$package_sku_id)->delete();
            $sql = "update $stock_db set stock = stock + 1, sales = sales - 1 where id = ".$sku_item->id;
            DB::update($sql);
        }

        return $package_sku_id;
    }

    function get_rand($proArr) {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);             //抽取随机数
            if ($randNum <= $proCur) {
                $result = $key;                         //得出结果
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }


    // 试一试开盒结果
    public function showTry(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        $cached = Cache::get($uuid) ?: ThrowException::Conflict('页面失效~');

        $info = [
            'skus' => @$cached['skus'] ?: []
        ];

        return $info;
    }

    // 开启盒子
    public function open(Request $req)
    {
        $user = Auth::requireLoginUser();

        // 使用订单id开启
        if ($req->input('order_id')) {
            $package = $user->packages()->where('pay_order_id', $req->order_id)->first() ?: ThrowException::Conflict('package不存在~');
        }
        elseif ($req->input('package_id')) {
            $package = $user->packages()->where('id', $req->package_id)->first() ?: ThrowException::Conflict('package不存在~');
        }
        else {
            ThrowException::Conflict('请指定订单id或package id');
        }

        // 开启盒子
        $package->openBox();

        $info = $package->getInfo([
            'id',
            'uuid',
            'union_status',
            'union_status_text'
        ]);

        // 仅已开盒的package可查看sku列表
        if ($package->union_status === 'opened') {
            // 仅返回的字段
            $only = [
                'id',
                'uuid',
                'title',
                'number',
                'thumb',
                'money_price',
                'score_price',
                'total',
                'union_status',
                'union_status_text',
                'sid'
            ];

            $info['skus'] = filtList($package->skus, $only);
        }
        else {
            $info['skus'] = [];
        }

        return $info;
    }
}
